﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
namespace project
{
    public partial class AdminControl : UIPage
    {
        public static Action updateDataGridView;
        public AdminControl()
        {
            InitializeComponent();

            this.Size = new System.Drawing.Size(1271, 631);
            this.uiDataGridView1.Size = new System.Drawing.Size(1201, 561);
            uiDataGridView1.AllowUserToAddRows = false;
            uiDataGridView1.ReadOnly = true;
            uiDataGridView1.MultiSelect = false;
            uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //查询
                string sqlStr = $"select * from admin_info where admin_number like '%{uiTextBox1.Text}%'";

                uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

                // 设置表格上显示的列标题

        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            //修改
             int selectCount = this.uiDataGridView1.SelectedRows.Count;
            if (selectCount != 0)
            {
                string id = uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string pwd = uiDataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                EditAdmin adminEditForm = new EditAdmin(id, pwd );
                adminEditForm.ShowDialog();
                QueryAllBook();
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            //删除
            int selectCount = uiDataGridView1.SelectedRows.Count;
            if (selectCount != 0)
            {
                if (MessageBox.Show("你确认要删除当前选中行吗？此操作不可回退", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    int id = int.Parse(uiDataGridView1.SelectedRows[0].Cells[0].Value.ToString());

                    string sqlStr = $"delete from admin_info where admin_number={id}";

                    int result = SqlHelper.ExecuteNonQuery(sqlStr);
                    if (result != 0)
                    {
                        MessageBox.Show("删除成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        QueryAllBook();
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择正确的数据", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            //添加
            AddAdmin a = new AddAdmin();
            a.ShowDialog();
            QueryAllBook();
        }

        private void AdminControl_Load(object sender, EventArgs e)
        {
            updateDataGridView = QueryAllBook;
            QueryAllBook();

        } protected void QueryAllBook()
        {
            string sqlStr = "select * from admin_info";

            // 把DataGridView的数据源绑定到DataSet上的第一个表中
            uiDataGridView1.DataSource = SqlHelper.ExcuteDataSet(sqlStr).Tables[0];

            // 设置表格上显示的列标题
            uiDataGridView1.Columns[0].HeaderText = "编号";
            uiDataGridView1.Columns[1].HeaderText = "密码";
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
